package com.bitcarton.disso.core.utils;

import org.springframework.util.StreamUtils;

import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * @author wwd
 */
public class HttpUtil {

    /**
     *
     * @param httpURL
     * @param params
     * @return
     * @throws Exception
     */
//    public static String sendHttpRequest(String httpURL, Map<String,String> params) throws Exception{
//        URL url = new URL(httpURL);
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        conn.setRequestMethod("POST");
//        conn.setDoOutput(true);
//        if(params!=null&&params.size()>0){
//            StringBuilder sb = new StringBuilder();
//            for(Map.Entry<String,String> entry:params.entrySet()){
//                sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
//            }
//            conn.getOutputStream().write(sb.substring(1).toString().getBytes("utf-8"));
//        }
//        conn.connect();
//        String responseContent = StreamUtils.copyToString(conn.getInputStream(), Charset.forName("utf-8"));
//        conn.disconnect();
//        return responseContent;
//    }

//    public static String sendRequest(String httpURL) throws Exception{
//        URL url = new URL(httpURL);
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        conn.setRequestMethod("POST");
//        conn.setDoOutput(true);
//        conn.connect();
//        String responseContent = StreamUtils.copyToString(conn.getInputStream(), Charset.forName("utf-8"));
//        conn.disconnect();
//        return responseContent;
//    }

    /**
     *
     * @param httpURL
     * @param jesssionId
     * @throws Exception
     */
//    public static void sendHttpRequest(String httpURL,String jesssionId) throws Exception{
//        URL url = new URL(httpURL);
//        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        conn.setRequestMethod("POST");
//        conn.setDoOutput(true);
//        conn.addRequestProperty("Cookie","JSESSIONID="+jesssionId);
//        conn.connect();
//        conn.getInputStream();
//        conn.disconnect();
//    }
}
